M = {}
local moveDirection
local Score = 0
-- top,down,left,right
function init()
    for i=1,4 do
        M[i] = {}
        for j=1,4 do
            M[i][j] = 0
        end
    end
    randomInput()
    randomInput()
end

function randomInput( ... )
    local bool = math.random(1,2)
    if bool == 2 then v = 2 else v = 4 end
    local i = math.random(1,4)
    local j = math.random(1,4)
    if M[i][j] == 0 then
        M[i][j] = v
        print("--------add new numbers at",i,j,"-------")
    else
        randomInput(...)
    end
end

function checkCellEqual()
    moveDirection = tonumber(io.read())
    print("Direction is",moveDirection)
    switch = {
        [1] = function ( ... )
            -- top, i change from 4 to 1, j unchanged 
            top()
            randomInput()
        end,
        [2] = function ( ... )
            -- down,i change from 1 to 4, j unchanged 
            down()
            randomInput()
        end,
        [3] = function ( ... )
            -- left, j change from 4 to 1, i unchanged
            left()
            randomInput()
        end,
        [4] = function ( ... )
            -- right, j change from 1 to 4, i unchanged
            right()
            randomInput()
        end,
    }
    switch[moveDirection]()
    printMatrix()
    checkCellEqual()
end


function left( ... )
    print("merging left")
    for i=1,4 do
        for j=1,4 do
            k = 4 - j
            -- k is a control number
            if k == 0 then break end
            if M[i][j] ~= 0 and M[i][j] == M[i][k] then
                print(j,"equal",k,"on row",i)
                print("---------------starting merging cells--------------")
                M[i][j] = M[i][j] + M[i][k]
                M[i][k] = 0
                Score = Score + M[i][j]
            end
        end
    end
end

function right( ... )
    print("merging right")
    for i=1,4 do
        for j=1,4 do
            k = 4 - j
            -- k is a control number
            if k == 0 then break end
            if M[i][j] ~= 0 and M[i][j] == M[i][k] then
                print(j,"equal",k,"on row",i)
                print("---------------starting merging cells--------------")
                M[i][k] = M[i][j] + M[i][k]
                M[i][j] = 0
                Score = Score + M[i][k]
            end
        end
    end
end

function top( ... )
    print("merging up")
    for i=1,4 do
        k = 4 - i
        for j=1,4 do
            -- k is a control number
            if k == 0 then break end
            if M[i][j] ~= 0 and M[i][j] == M[k][j] then
                print(i,"equal",k,"on column",j)
                print("---------------starting merging cells--------------")
                M[i][j] = M[i][j] + M[k][j]
                M[k][j] = 0
                Score = Score + M[i][j]
            end
        end
    end
end

function down( ... )
    print("merging down")
    for i=1,4 do
        k = 4 - i
        for j=1,4 do
            -- k is a control number
            if k == 0 then break end
            if M[i][j] ~= 0 and M[i][j] == M[k][j] then
                print(i,"equal",k,"on column",j)
                print("---------------starting merging cells--------------")
                M[k][j] = M[i][j] + M[k][j]
                M[i][j] = 0
                Score = Score + M[k][j]
            end
        end
    end
end

function printMatrix( ... )
    print("Now your Score is",Score)
    -- print("i","-","-","-","-","-")
    -- print("-","j","1","2","3","4")
    print("-","-","-","-")
    for i=1,4 do
        print(M[i][1],M[i][2],M[i][3],M[i][4])
    end
    print("-","-","-","-")
end

function gameInit()
    init()
    printMatrix()
    moveDirection = math.random(1,4)
end

gameInit()
checkCellEqual()
